# -*- encoding=utf8 -*-
        
__author__ = "Bryan.Chen"
__desc__=""
        
from airtest.core.api import *
        
auto_setup(__file__)
        
from poco.drivers.std import StdPoco
poco = StdPoco()
        
using("../FlowTest.air")
import FlowTest
using("SideMenu_Open.air")
import SideMenu_Open

import random
        
class SideMenu_SwitchArea(FlowTest.BaseTestCase):
    def __init__(self):
        print("%s __init__" % (__file__))
        
    def runTest(self, args):
        print("%s runTest" % (__file__))
        areaName = None
        if "areaName" in args:
            areaName = args["areaName"]
        # 打开左侧菜单
        isOpenSideMenu = SideMenu_Open.runTest({})
        if not isOpenSideMenu:
            return False

        # 获取左侧菜单节点
        bottomPanel = poco("bottom_panel")
        if not bottomPanel.exists():
            return False
        # 获取切换地区按钮
        btnChangeLocation = bottomPanel.offspring("panel_location_touch")
        if not btnChangeLocation.exists():
            return False

        # 点击切换地区
        self.clickButton(btnChangeLocation)

        try:
            # 等待切换地区界面显示
            poco("LocationView").wait_for_appearance()
        except:
            assert_equal(False, True, "地区选择界面弹出正常")
            return False

        # 获取地区选择界面
        locationView = poco("LocationView")
        if not locationView.exists():
            return False
            
        # 获取热门地区
        hotItems = locationView.offspring("button_ad_temp")
        strLocation = ""
        if len(hotItems) > 0:
            if not areaName:
                # 生成随机数，随机选一个地区切换
                n = random.randint(0, len(hotItems) - 1)
                strLocation = hotItems[n].get_text()
                self.clickButton(hotItems[n])
            else:
                for i in range(0, len(hotItems)):
                    strLocation = hotItems[i].get_text()
                    if strLocation == areaName:
                        strLocation = areaName
                        self.clickButton(hotItems[i])
                        break

        print(strLocation)

        # 等待1s，待地区选择界面完全隐藏再操作
        sleep(1.0)
        # 获取新地区进行比对
        txtLocationName = bottomPanel.offspring("text_location_name")
        strNewLocation = ""
        if txtLocationName.exists():
            strNewLocation = txtLocationName.get_text()
            print(strNewLocation)
        assert_equal(strNewLocation, strLocation, "切换地区后显示正常")

        # 点击屏幕右侧，隐藏左侧菜单
        poco.click([0.7, 0.5])

        return True
        
_instance = SideMenu_SwitchArea()
runTest = _instance.runTest
